﻿// *******************************************************************************************************
// Example code to show how to read and write to a 24C32 EEPROM using the Devantech USB-I2C Module.
// Writen in Visual C# Express 2008.
// Before using this code you must have installed the FTDI VCP drivers. 
// The VCP drivers make the USB-I2C appear as a serial Port on your PC.
// See http://www.robot-electronics.co.uk/htm/usb_i2c_tech.htm
// NOTE!   This code contains no error handling. 
//         It's point is to show how to use C#, the USB-I2C and an EEPROM.
// *******************************************************************************************************
// The 24C32 uses a 2-byte internal register address
// *******************************************************************************************************

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO.Ports;
using System.Threading;

namespace EE24C32
{
     public partial class EE24C32 : Form
    {
       static SerialPort USB_I2C;

        public EE24C32()
        {
           InitializeComponent();

            USB_I2C = new SerialPort();
            // Get a list of serial port names.
            foreach (string s in SerialPort.GetPortNames())
            {
                Ports.Items.Add(s);
            }
            Message.Text = "Select Com port";
        }

        private void Ports_SelectedIndexChanged(object sender, EventArgs e)
        {
            byte[] SerBuf = new byte[10];
            string response;
            USB_I2C.PortName = Ports.Text;
            USB_I2C.Parity = 0;
            USB_I2C.BaudRate = 9600;
            USB_I2C.StopBits = StopBits.Two;
            USB_I2C.DataBits = 8;
            USB_I2C.ReadTimeout = 500;
            USB_I2C.WriteTimeout = 500;
            USB_I2C.Open();
        //    USB_I2C.WriteLine("\r");
        //    Thread.Sleep(100);
            USB_I2C.WriteLine("\r");
            Thread.Sleep(50);
            try {
            response = USB_I2C.ReadLine().ToString(); 
	        } catch { }
            USB_I2C.WriteLine("A 0\r");
            Thread.Sleep(50);
            try {
            response = USB_I2C.ReadLine().ToString(); 
	        } catch { }
            USB_I2C.WriteLine("D\r");
            Thread.Sleep(50);
            try {
            response = USB_I2C.ReadLine().ToString(); 
	        } catch { }
            //USB_I2C.WriteLine("s 90");
            //Thread.Sleep(50);
            //try
            //{
            //    response = USB_I2C.ReadLine().ToString();
            //}
            //catch { }
            //SerBuf[0] = 0x5A;    // USB_I2C Module Command
            //SerBuf[1] = 0x01;     // Get Revision
            //SerBuf[2] = 0;
            //SerBuf[3] = 0;
            //USB_I2C.Write(SerBuf, 0, 4);
            //USB_I2C.Read(SerBuf, 0, 1);
            //USB_I2C_VER.Text = string.Format("{0}", SerBuf[0]);
            //ReadAddr.Text = "00";
            //WriteAddr.Text = "00";
            //ReadEEPROM();
        }

        private void colour_select_Click(object sender, EventArgs e)
        {
            byte[] SerBuf = new byte[10];
            string i2c_command;
            string response;
            //string[] PArray;
            colorDialog1.ShowDialog();
            this.BackColor = colorDialog1.Color;

            //SerBuf[0] = 0x55;                    // Command for Devices with 2 address bytes 
            //SerBuf[1] = 0x00;                    // 24C32 I2C address + low bit set to indicate its a read operation
            //SerBuf[2] = 0x6e;         // 24C32 high byte of internal address to read from
            //SerBuf[3] = 0x03;         // 24C32 high byte of internal address to read from
            //SerBuf[4] = 0xff;             // 24C32 low byte of internal address to read from
            //SerBuf[5] = 0x00;                       // number of bytes to read
            //SerBuf[6] = 0xff;                       // number of bytes to read
            //USB_I2C.Write(SerBuf, 0, 7);         // send the read command to the USB-I2C module
            ////    while (USB_I2C.BytesToRead < 1) ; 
       //     while (USB_I2C.BytesToRead < 1) ;      // wait for data to arrive from the EEPROM
     //       SerBuf[0] = 0x53;                    // Command for Devices with 2 address bytes 
      //      SerBuf[1] = 0x00;                    // 24C32 I2C address + low bit set to indicate its a read operation
      //      SerBuf[2] = 0x6f;         // 24C32 high byte of internal address to read from
       //     USB_I2C.Write(SerBuf, 0, 3);
        //    while (USB_I2C.BytesToRead < 1); 
       //     USB_I2C.Read(SerBuf, 0, 1);
            //string colourR = System.Text.ASCIIEncoding.ASCII.GetString(colorDialog1.Color.R);

             //USB_I2C.WriteLine("\r");
           // Thread.Sleep(200);
            i2c_command = string.Format("s 99 {0} {1} {2} p", (byte)colorDialog1.Color.R, (byte)colorDialog1.Color.G, (byte)colorDialog1.Color.B);
            //PArray[0] = "\r";
            //PArray[1] = "s";
            //PArray[2] = "6e";
            //PArray[3] = colorDialog1.Color.R;
            //PArray[4] = colorDialog1.Color.G;
            //PArray[5] = colorDialog1.Color.B;
            //PArray[6] = "p";
            //PArray[7] = "\r";
        //   i2c_command = "\r s 6e " . colorDialog1.Color.R . " p \r";

          //   \r s 6e ff ff ff p \r
          //13 73 6e ff ff ff 70 13 
            //SerBuf[0] = 0x13;                    // Command for Devices with 2 address bytes 
            //SerBuf[1] = 0x73;                    // 24C32 I2C address + low bit set to indicate its a read operation
            //SerBuf[2] = 0x6e;         // 24C32 high byte of internal address to read from
            //SerBuf[3] = 0xff;         // 24C32 high byte of internal address to read from
            //SerBuf[4] = 0xff;         // 24C32 high byte of internal address to read from
            //SerBuf[5] = 0xff;             // 24C32 low byte of internal address to read from
            //SerBuf[6] = 0x70;                       // number of bytes to read
            //SerBuf[7] = 0x13;                       // number of bytes to read

       //  i2c_command = string.Join(" ", PArray);
            //SerBuf[0] = 's';                    // Command for Devices with 2 address bytes 
            //SerBuf[1] = 0x00;                    // 24C32 I2C address + low bit set to indicate its a read operation
            //SerBuf[2] = 0x6e;         // 24C32 high byte of internal address to read from
            //SerBuf[3] = 0x04;         // 24C32 high byte of internal address to read from
            //SerBuf[4] = 0x6e;         // 24C32 high byte of internal address to read from
            //SerBuf[5] = 0x05;             // 24C32 low byte of internal address to read from
            //SerBuf[6] = 0x05;                       // number of bytes to read
            //SerBuf[7] = 0x05;                       // number of bytes to read
            //SerBuf[4] = colorDialog1.Color.R;             // 24C32 low byte of internal address to read from
            //SerBuf[5] = colorDialog1.Color.G;                       // number of bytes to read
            //SerBuf[6] = colorDialog1.Color.B;  
            USB_I2C.WriteLine(i2c_command);   // number of bytes to read
            Thread.Sleep(5);
            try {
            response = USB_I2C.ReadLine().ToString();
          //  ReadData.Text = string.Format("{0}", response);
	        } catch { }
      //      USB_I2C.WriteLine("\r s 6e ff ff ff p \r");
          //  USB_I2C.Write(SerBuf, 0, 8);         // send the read command to the USB-I2C module
          //  while (USB_I2C.BytesToRead < 1) ;      // wait for data to arrive from the EEPROM
         //   USB_I2C.Read(SerBuf, 0, 1);     // get the result of write operation
            //SerBuf[0] = 0x53;                    // Command for Devices with 2 address bytes 
            //SerBuf[1] = 0x90;                    // 24C32 I2C address + low bit set to indicate its a read operation
            //SerBuf[2] = 0xee;         // 24C32 high byte of internal address to read from
            //USB_I2C.Write(SerBuf, 0, 3);
            //while (USB_I2C.BytesToRead < 1) ;      // wait for data to arrive from the EEPROM
            //SerBuf[0] = 0x55;                    // Command for Devices with 2 address bytes 
            //SerBuf[1] = 0x91;                    // 24C32 I2C address + low bit set to indicate its a read operation
            //SerBuf[2] = 0xaa;         // 24C32 high byte of internal address to read from
            //SerBuf[3] = 0x02;         // 24C32 high byte of internal address to read from
            //USB_I2C.Write(SerBuf, 0, 4);
            //while (USB_I2C.BytesToRead < 2) ;
            //Message.Text = string.Format("{0} {1} {2} {3}", (byte)colorDialog1.Color.R, (byte)colorDialog1.Color.G, (byte)colorDialog1.Color.B, SerBuf[0]);
            
            Message.Text = i2c_command;
        }


    }
}
